﻿using System;
using System.Collections.Generic;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Doodads
{

    public partial class Subscribe : DoodadBaseClass
    {

        new protected void Page_Load(object sender, EventArgs e)
        {

            base.Page_Load(sender, e);

            if (IsPostBack)
                return;

            HTMLLiteral.Text = Properties.DoodadInstanceHTML;

            if (ParentPage.EditMode && ParentPage.EmployeeLoggedIn)
            {
                HTMLPanel.CssClass = "editable";
                HTMLPanel.Attributes.Add("data-id", Properties.ElementID);
            }

        }

        protected void SubscribeLinkButton_Click(object sender, EventArgs e)
        {


            if (!MiscUtils.IsValidEmailAddress(SubscribeEmailTextBox.Text))
            {
                ErrorLabel.Visible = true;
                return;
            }

            try
            {

                Dictionary<string, object> args = new Dictionary<string, object>();
                args.Add("WebsiteAPIKey", PageBaseClass.WebsiteAPIKey);
                args.Add("EmailAddress", SubscribeEmailTextBox.Text);
                APIEnvelope env = Soveryn.Commerce.Shared.API.Call(PageBaseClass.EmarketingAPIURL, "EmarketingEmailRegister", args, false, false);
            }
            catch { }

            SuccessLabel.Visible = true;

        }

    }
}
